package org.itboys.mobile.controller.weixin;

import com.wordnik.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dao.wallet.CooWalletRecordMapper;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.wallet.CooWallet;
import org.itboys.mobile.entity.mysql.wallet.CooWalletRecord;
import org.itboys.mobile.service.mongo.web.member.SundayMemberService;
import org.itboys.mobile.service.mysql.weixin.wallet.CooWeixinWalletService;
import org.itboys.mobile.util.MemberUtil;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * 作者：zzq
 * 时间：2018/2/4
 * 描述：
 */
@Controller
@RequestMapping("/test")
public class testController extends BaseController{
    @Autowired
    private CooWeixinWalletService weixinWalletService;
    @Autowired
    private CooWalletRecordMapper walletRecordMapper;
    @Autowired
    private SundayMemberService memberService;
    @RequestMapping("/test")
    public void setDefault(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "recordId", required = true)Long  recordId){
        try{
            //获取session中的用户信息
            CooWalletRecord walletRecord=walletRecordMapper.find(recordId);
            walletRecord.setAmount(walletRecord.getAmount().multiply(new BigDecimal("100000")));
            weixinWalletService.charge_callback(walletRecord,"充值消费-微信付款-支付编号"+"测试编号");
            ResponseMessage.success();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @RequestMapping("/update")
    public void update(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "id", required = true)Long  id,
            @RequestParam(value = "recommendId", required = true)Long  recommendId){
        try{
            SundayMember member=memberService.getById(id);
            member.setRecommendId(recommendId);
            SundayMember tjMember=memberService.getById(recommendId);
            //继承推荐人的销售（推荐）链 并且加上自己的id
            String salesPath = StringUtils.isNotEmpty(tjMember.getSalesPath())?tjMember.getSalesPath()+"/"+member.getId():String.valueOf(member.getId());
            member.setSalesPath(salesPath);
            //继承推荐人的管理链 并且加上自己的id
            String managerPath = StringUtils.isNotEmpty(tjMember.getManagerPath())?tjMember.getManagerPath()+"/"+member.getId():String.valueOf(member.getId());
            member.setManagerPath(managerPath);
            //增加推荐人的下级推荐链
            String firstPath=StringUtils.isNotEmpty(tjMember.getFirstPath())?
                    tjMember.getFirstPath()+"/"+member.getId():
                    String.valueOf(member.getId());
            tjMember.setFirstPath(firstPath);

            CooWallet wallet =weixinWalletService.findByMemberId(member.getId());
            wallet.setSalesId(member.getRecommendId());
            wallet.setManagerId(member.getRecommendId());
            weixinWalletService.update(wallet);
            memberService.updateExceptEmpty(member.getId(),member);
            memberService.updateExceptEmpty(tjMember.getId(),tjMember);
            ResponseMessage.success();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}


